package xs.szw.common.result;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @program: My_SpringSecurity
 * @description:
 * @author: Songzw
 * @create: 2025-02-06 14:34
 **/
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {

    /**
     * 状态码
     */
    private String code;

    /**
     * 描述信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     *  时间戳
     */
    private String localDateTime ;


    public Result(){
        this.localDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 调用成功
     * @param data 返回数据
     * @return     返回结果
     * @param <T>  返回类型
     */
    public static <T> Result<T> success(T data) {
        return (Result<T>) new Result<>().setCode(ResultCode.RC200.getCode()).setMessage(ResultCode.RC200.getMessage()).setData(data);
    }
    public static <T> Result<T> success() {
        return (Result<T>) new Result<>().setCode(ResultCode.RC200.getCode()).setMessage(ResultCode.RC200.getMessage()).setData(null);
    }

    public static <T> Result<T> success(T data,String message) {
        return (Result<T>) new Result<>().setCode(ResultCode.RC200.getCode()).setMessage(message).setData(data);
    }

    public static <T> Result<T> success(T data,String code,String message) {
        return (Result<T>) new Result<>().setCode(code).setMessage(message).setData(data);
    }

    /**
     * 调用失败
     * @param code  状态码
     * @param message 错误信息
     * @return  返回结果
     * @param <T> 返回类型
     */
    public static <T> Result<T> fail(String code, String message) {
        return (Result<T>) new Result<>().setCode(code).setMessage(message);
    }

    public static <T> Result<T> fail() {
        return (Result<T>) new Result<>().setCode(ResultCode.RC500.getCode()).setMessage(ResultCode.RC500.getMessage());
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return (Result<T>) new Result<>().setCode(resultCode.getCode()).setMessage(resultCode.getMessage());
    }

    public static <T> Result<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return fail();
        }
    }

    @Serial
    private static final long serialVersionUID = 1L;

}
